﻿#include <iostream>
#include <vector>
#include<queue>
#include<algorithm>
using namespace std;
class Solution {
public:
    void sortColors(vector<int>& nums)
    {
        int left = -1, i = 0, right = nums.size();
        while (i < right)
        {
            if (nums[i] == 0) swap(nums[++left], nums[i++]);
            else if (nums[i] == 1) i++;
            else swap(nums[--right], nums[i]);
        }
    }
};

class Solution {
public:
    void qsort(vector<int>& nums, int l, int r)
    {
        if (l >= r) return;
        int key = getRandom(nums, l, r);
        //0 -1 n
        int i = l, left = l - 1, right = r + 1;
        while (i < right)
        {
            if (nums[i] < key) swap(nums[++left], nums[i++]);
            else if (nums[i] == key) i++;
            else swap(nums[--right], nums[i]);
        }

        // [l, left] [left + 1, right - 1] [right, r]
        qsort(nums, l, left);
        qsort(nums, right, r);

    }
    int getRandom(vector<int>& nums, int left, int right)
    {
        int r = rand();
        return nums[r % (right - left + 1) + left];
    }
    vector<int> sortArray(vector<int>& nums)
    {
        srand(time(NULL)); // 种下⼀个随机数种⼦
        qsort(nums, 0, nums.size() - 1);
        return nums;
    }
};

class Solution {
public:
    int findKthLargest(vector<int>& nums, int k)
    {
        // 将数组中的元素先放入优先级队列中
        priority_queue<int> p(nums.begin(), nums.end());
        // 将优先级队列中前k-1个元素删除掉
        for (int i = 0; i < k - 1; ++i)
        {
            p.pop();
        }
        return p.top();
    }
};

class Solution {
public:
    void AdjustDown(vector<int>& nums, int n, int parent)
    {
        int child = parent * 2 + 1;

        while (child < n) 
        {
            if (child + 1 < n && nums[child + 1] < nums[child])
            {
                ++child;
            }

            if (nums[child] < nums[parent])
            {
                swap(nums[child], nums[parent]);
                parent = child;
                child = parent * 2 + 1;
            }
            else
            {
                break;
            }
        }
    }

    void HeapSort(vector<int>& nums, int n)
    {
        // 向下调整建堆 O(N)
        for (int i = (n - 1 - 1) / 2; i >= 0; i--)
        {
            AdjustDown(nums, n, i);
        }

        // O(N*logN)
        int end = n - 1;
        while (end > 0)
        {
            swap(nums[0], nums[end]);
            AdjustDown(nums, end, 0);
            --end;
        }
    }
    int findKthLargest(vector<int>& nums, int k)
    {
        //建大堆
        HeapSort(nums, nums.size());
        nums.erase(nums.begin(), nums.begin() + k - 1);
        return nums.front();
    }
};

class Solution {
public:
    int GetRandom(vector<int>& nums, int left, int right)
    {
        int r = rand();
        return nums[r % (right - left + 1) + left];
    }
    int quickselectsort(vector<int>& nums, int l, int r, int k)
    {
        if (l == r) return nums[l];
        int key = GetRandom(nums, l, r);
        int i = l, left = l - 1, right = r + 1;
        while (i < right)
        {
            if (nums[i] < key) swap(nums[++left], nums[i++]);
            else if (nums[i] == key) i++;
            else swap(nums[--right], nums[i]);
        }

        //分区域
        int c = r - right + 1, b = right - left - 1;
        if (c >= k) return quickselectsort(nums, right, r, k);
        else if (b + c >= k) return key;
        else return quickselectsort(nums, l, left, k - b - c);
    }
    int findKthLargest(vector<int>& nums, int k)
    {
        srand(time(nullptr));
        return quickselectsort(nums, 0, nums.size() - 1, k);
    }
};

class Solution {
public:
    int GetRandom(vector<int>& nums, int left, int right)
    {
        int r = rand();
        return nums[r % (right - left + 1) + left];
    }
    void quickselectsort(vector<int>& nums, int l, int r, int k)
    {
        if (l >= r) return;
        int key = GetRandom(nums, l, r);
        int i = l, left = l - 1, right = r + 1;
        while (i < right)
        {
            if (nums[i] < key) swap(nums[++left], nums[i++]);
            else if (nums[i] == key) i++;
            else swap(nums[--right], nums[i]);
        }
        // [l, left][left + 1, right - 1] [right, r]
        // 2. 分情况讨论
        int a = left - l + 1, b = right - left - 1;
        if (a > k) quickselectsort(nums, l, left, k);
        else if (a + b >= k) return;
        else quickselectsort(nums, right, r, k - a - b);
    }
    vector<int> inventoryManagement(vector<int>& nums, int k)
    {
        srand(time(nullptr));
        quickselectsort(nums, 0, nums.size() - 1, k);
        return { nums.begin(),nums.begin() + k };
    }
};

class Solution {
public:
    //归并排序
    vector<int> tmp;
    void meragesort(vector<int>& nums, int left, int right)
    {
        if (left >= right) return;
        int mid = (left + right) >> 1;

        //[left , mid][mid+ 1,right]
        meragesort(nums, left, mid);
        meragesort(nums, mid + 1, right);

        int cur1 = left, cur2 = mid + 1, i = 0;
        while (cur1 <= mid && cur2 <= right)
        {
            tmp[i++] = nums[cur1] <= nums[cur2] ? nums[cur1++] : nums[cur2++];
        }

        while (cur1 <= mid) tmp[i++] = nums[cur1++];
        while (cur2 <= right) tmp[i++] = nums[cur2++];

        //还原
        for (int i = left; i <= right; i++)
        {
            nums[i] = tmp[i - left];
        }
    }
    vector<int> sortArray(vector<int>& nums)
    {
        tmp.resize(nums.size());
        meragesort(nums, 0, nums.size() - 1);
        return nums;
    }
};


//升序利用归并解决逆序对问题
class Solution {
public:
    int tmp[50001];
    int meragesort(vector<int>& nums, int left, int right)
    {
        if (left >= right) return 0;
        int mid = (left + right) >> 1;

        int ret = 0;
        //[left , mid][mid+ 1,right]
        ret += meragesort(nums, left, mid);
        ret += meragesort(nums, mid + 1, right);

        int cur1 = left, cur2 = mid + 1, i = 0;
        while (cur1 <= mid && cur2 <= right)
        {
            if (nums[cur1] <= nums[cur2])
            {
                tmp[i++] = nums[cur1++];
            }
            else
            {
                //nums[cur1] > nums[cur2]
                //策略1：找出该数之前，有多少个数比我大
                ret += mid - cur1 + 1;
                tmp[i++] = nums[cur2++];
            }
        }

        while (cur1 <= mid) tmp[i++] = nums[cur1++];
        while (cur2 <= right) tmp[i++] = nums[cur2++];

        //还原
        for (int i = left; i <= right; i++)
        {
            nums[i] = tmp[i - left];
        }
        return ret;
    }
    int reversePairs(vector<int>& record)
    {
        return meragesort(record, 0, record.size() - 1);
    }
};

//降序利用归并解决逆序对问题
class Solution {
public:
    int tmp[50001];
    int meragesort(vector<int>& nums, int left, int right)
    {
        //只有一个元素 没有逆序对
        if (left >= right) return 0;
        int mid = (left + right) >> 1;

        int ret = 0;
        //[left , mid][mid+ 1,right]
        ret += meragesort(nums, left, mid);
        ret += meragesort(nums, mid + 1, right);

        int cur1 = left, cur2 = mid + 1, i = 0;
        while (cur1 <= mid && cur2 <= right)
        {
            if (nums[cur1] > nums[cur2])
            {
                ret += right - cur2 + 1;
                tmp[i++] = nums[cur1++];
            }
            else
            {
                tmp[i++] = nums[cur2++];
            }
        }

        while (cur1 <= mid) tmp[i++] = nums[cur1++];
        while (cur2 <= right) tmp[i++] = nums[cur2++];

        //还原
        for (int i = left; i <= right; i++)
        {
            nums[i] = tmp[i - left];
        }
        return ret;
    }
    int reversePairs(vector<int>& record)
    {
        return meragesort(record, 0, record.size() - 1);
    }
};

//计算右侧⼩于当前元素的个数
class Solution {
public:
    vector<int> ret;
    vector<int> index;//// 记录nums中当前元素的原始下标
    int tmpNums[500010];
    int tmpIndex[500010];

    vector<int> countSmaller(vector<int>& nums)
    {
        ret.resize(nums.size());
        index.resize(nums.size());
        for (int i = 0; i < nums.size(); i++)
        {
            index[i] = i;
        }
        MerageSort(nums, 0, nums.size() - 1);

        return ret;
    }

    void MerageSort(vector<int>& nums, int left, int right)
    {
        if (left >= right) return;
        int mid = (left + right) >> 1;

        MerageSort(nums, left, mid);
        MerageSort(nums, mid + 1, right);

        int cur1 = left, cur2 = mid + 1, i = 0;
        //降序
        while (cur1 <= mid && cur2 <= right)
        {
            if (nums[cur1] <= nums[cur2])
            {
                tmpNums[i] = nums[cur2];
                tmpIndex[i++] = index[cur2++];
            }
            else
            {
                //cur1对应的下标ret[1]+= 小于当前元素的个数
                ret[index[cur1]] += right - cur2 + 1;
                tmpNums[i] = nums[cur1];
                tmpIndex[i++] = index[cur1++];
            }
        }

        while (cur1 <= mid)
        {
            tmpNums[i] = nums[cur1];
            tmpIndex[i++] = index[cur1++];
        }
        while (cur2 <= right)
        {
            tmpNums[i] = nums[cur2];
            tmpIndex[i++] = index[cur2++];
        }
        //还原
        for (int j = left; j <= right; j++)
        {
            nums[j] = tmpNums[j - left];
            index[j] = tmpIndex[j - left];
        }
    }
};

//翻转对
class Solution {
public:
    int tmp[50010];
    //先计算翻转对再合并有序数组
    int MerageSort(vector<int>& nums, int left, int right)
    {
        if (left >= right) return 0;

        int mid = (left + right) >> 1;

        int ret = 0;
        ret += MerageSort(nums, left, mid);
        ret += MerageSort(nums, mid + 1, right);
        int cur1 = left, cur2 = mid + 1, i = left;
        while (cur1 <= mid)
        {
            //计算当前元素后面，有多少元素的两倍比我小
            //降序
            while (cur2 <= right && nums[cur2] >= nums[cur1] / 2.0) cur2++;
            if (cur2 > right) break;
            //nums[cur2] <  nums[cur1] / 2.0
            ret += right - cur2 + 1;
            cur1++;
        }

        cur1 = left, cur2 = mid + 1;
        while (cur1 <= mid && cur2 <= right)
        {
            if (nums[cur1] <= nums[cur2])
            {
                tmp[i++] = nums[cur2++];
            }
            else
            {
                tmp[i++] = nums[cur1++];
            }
        }

        while (cur1 <= mid) tmp[i++] = nums[cur1++];
        while (cur2 <= right) tmp[i++] = nums[cur2++];

        for (int i = left; i <= right; i++)
        {
            nums[i] = tmp[i];
        }
        return ret;
    }
    int reversePairs(vector<int>& nums) {
        return MerageSort(nums, 0, nums.size() - 1);
    }
};


//翻转对
//升序版本
class Solution {
public:
    int tmp[50010];
    //先计算翻转对再合并有序数组
    int MerageSort(vector<int>& nums, int left, int right)
    {
        if (left >= right) return 0;

        int mid = (left + right) >> 1;

        int ret = 0;
        ret += MerageSort(nums, left, mid);
        ret += MerageSort(nums, mid + 1, right);
        int cur1 = left, cur2 = mid + 1, i = left;
        while (cur2 <= right)
        {
            //升序
            while (cur1 <= mid && nums[cur1] / 2.0 <= nums[cur2]) cur1++;
            if (cur1 > mid) break;
            //计算当前元素之前，有多少元素的一半比cur2(我)大 固定cur2
            //nums[cur1] / 2.0 >  nums[cur2] 更新结果
            ret += mid - cur1 + 1;
            cur2++;
        }
        cur1 = left, cur2 = mid + 1;
        while (cur1 <= mid && cur2 <= right)
        {
            if (nums[cur1] <= nums[cur2])
            {
                tmp[i++] = nums[cur1++];
            }
            else
            {
                tmp[i++] = nums[cur2++];
            }
        }

        while (cur1 <= mid) tmp[i++] = nums[cur1++];
        while (cur2 <= right) tmp[i++] = nums[cur2++];

        for (int i = left; i <= right; i++)
        {
            nums[i] = tmp[i];
        }
        return ret;
    }
    int reversePairs(vector<int>& nums) {
        return MerageSort(nums, 0, nums.size() - 1);
    }
};